/*
 *商家模块
 *订单管理
*
*/
;(function($){
  $.Order={
        //取消订单
        'cannelorder':function(e)
        {
          var orderid=(e !=false)?e:0;
          $.ajax(
          {
            type:'post',
            url:"/shop.php/Home/Order/cancelOrder",
            data:{'orderid':orderid},
            dataType:"json",
            success:function(message)
            {
              if (message.status==false)
              {
                alert(message.msg);
                window.location.reload();
                return '';
              };
              alert(message.msg);
              window.location.reload();
            } 
          }
          );
        },
         //删除订单
         'delorder':function(e)
         {
           var res=confirm('确定删除订单');
           if(res==false){return false;}
           if(e==false){alert('参数错误，稍后再试');return false;}
           var orderid=e;
           $.ajax({
            type:'GET',
            url:"/shop.php/Home/Order/delOrder",
            data:{'orderid':orderid},
            dataType:"json",
            success:function(message)
            {
             if (message.status)
             {
              alert(message.msg);
              window.location.reload();
              return false;
            }else{
              alert(message.msg);
              return false;
            }
          }

      })
       },
    //搜索订单+分页
    searchOrder:function()
    {
       
       var $starttime=$("#start").val();
       var $endtime=$("#end").val();
       var page=(arguments[0]==false)?1:arguments[0];
       //获取订单区域
      var orderarea=$("select[name='areatype'] option:selected").val();
    
       $.ajax({
        type:'POST',
        url:"/shop.php/Home/Order/Ordersearch",
        data:{"starttime":$starttime,"endtime":$endtime,"page":page,'orderarea':orderarea},
        dataType:'JSON',
        success:function(result)
        {
          var statu =result.status;
          var list=result.lists.orders;
          var $page=result.lists.pagearr;
          if(statu)
          {
            var $tbody=$('<tbody></tbody>');
              var $string='';
              $.each(list,function(key,order){
                $string+='<tr><td><input type="checkbox"></td>';
                $string+="<td>"+order.order_sn+"</td>";
                $string+="<td>"+order.create_time+"</td>";
                $string+="<td>"+order.pay_type+"</td>";
                $string+="<td>"+order.number+"</td>";
                $string+="<td>"+order.order_amount+"</td>";
                $string+="<td>"+order.itemname+"</td>";
                $string+="<td>"+order.orderstatus+"</td>";
                $string+='<td><div class="am-btn-toolbar"><div class="am-btn-group am-btn-group-xs"><span class="am-icon-pencil-square-o"><a href="/shop.php/Home/Order/getOrder/orderid/'+order.orderid+'">查看详情</a></span>&nbsp;<span class="am-icon-trash-o"></span><a href="javascript:void(0)" class="delorder" value="716" onclick="$.Order.delorder('+order.orderid+')"> 删除 </a>';
              })////遍历数据end
              $tbody.append($string);
              $($tbody).replaceAll(".ordertable tbody");
               //替换分页
              $(".count").html($page.count);
              $(".pagetotal").html($page.pagetotal);
              $(".currentpage").html($page.page);
              $('.prepage').attr('value',$page.prevpage);
              $('.nexpage').attr('value',$page.nextpage);
              $('.laspage').attr('value',$page.pagetotal);
          }else
          {
             
              $(".pagetotal").html($page.pagetotal);
              $(".count").html($page.count);
              $('.ordertable tbody').html('暂无数据');
              
          }
        }
       }) ///ajax结束 
    },
    //确认订单
    confirmorder:function(){
      var $orderid=$("input[name='orderid']").val();
      if($orderid ==false){ alert('网络繁忙，请稍后再试'); return false;}
      $.ajax({
        type:'post',
        url:'/shop.php/Home/Order/confirmOrder',
        data:{'orderid':$orderid},
        dataType:'json',
        success:function(message){
            switch(message.status)
            {
              case 1:
                alert(message.msg);
                window.location.reload();
              break;
              case 2:
                alert(message.msg);
                window.location.reload();
              break;
                alert(message.msg);
                window.location.reload();
              case 0:
                alert(message.msg);
                window.location.reload();
              break;
            }
        }
      })
    },
    //确认收货
    confirmreceipt:function()
    {
      var $orderid=$("input[name='orderid']").val();
      if($orderid ==false){ alert('网络繁忙，请稍后再试'); return false;}
      $.ajax({
        type:'post',
        dataType:'json',
        data:{'orderid':$orderid},
        url:'/shop.php/Home/Order/confirmreceipt',
        success:function(message){
            switch(message.status)
            {
              case 1:
                alert(message.msg);
                window.location.reload();
              break;
              case 2:
                alert(message.msg);
                window.location.reload();
              break;
                alert(message.msg);
                window.location.reload();
              case 0:
                alert(message.msg);
                window.location.reload();
              break;
            }
        }

      })
    },




     }
    //确认订单
    $('.confirmorder').on('click',function(){
      $.Order.confirmorder();
    })
    //确认收货
    $('.confirmreceipt').on('click',function(){
      $.Order.confirmreceipt();
    })
   })(jQuery)
//支付订单
;(function($){

 $("form").submit(function(e){
  e.preventDefault();
  var $orderid=$('.orderid').val();
  var $order_amount=$('.amount').val();

  $.ajax({
    type:'POST',
    url:"/shop.php/Home/Order/payOrder",
    data:{'orderid':$orderid,'order_amount':$order_amount},
    dataType:'json',
    success:function(mess){
      // var  result=JSON.parse(msg);
      var status=mess.status;
      if (status !='') {
        alert(mess.msg);
        window.location.reload();
      }else{
        alert(mess.msg);
      }

    }
  })
});
})(jQuery)
//商品管理
;(function($){
//筛选条件
  //公用ajax 晒需条件
  $.searchitem=function(page,keywords)
  {

     //获取搜索条件
     var page = arguments[0]?arguments[0] : 1;
     var keywords=arguments[1]?arguments[1] : '';
     var $typeid=$("select[name='typeid'] option:selected").val();
     var $brandid=$("select[name='brandid'] option:selected").val();
     var $platid=$("input[name='platid']:checked").val();
      //缓存类目和品牌
      var date = new Date();
      date.setTime(date.getTime() + (5* 60 * 1000));
      $.cookie('typeid', $typeid, { expires: date,path:'/',});
      $.cookie('brandid', $brandid, { expires: date,path:'/' });
      $.cookie('platid', $platid, { expires: date,path:'/' });
      $.ajax({
        type:'POST',
        url:"/shop.php/Home/Admin/ajaxItem",
        data:{"typeid":$typeid,"brandid":$brandid,"page":page,"keywords":keywords,'platid':$platid},
        dataType:"json",
        success:function(message){
         //解析json 数据
         if(message.status){
              var $page=message.pagearr; //底部分页
              var $items=message.item;//订单
              var $tbody=$('<tbody></tbody>');
              var $string='';
              $.each($items,function(key,item){
                $string+="<tr class=item_"+item.itemid+">";
                $string+='<td><input type="checkbox"></td>';
                $string+="<td>"+item.itemid+"</td>";
                $string+=(item.isspeci !=false)?"<td>"+'<a href="javascript:void(0)" class="bindaddLayer" value="'+item.itemid +'"><span class=" am-icon-plus-square-o"></span></a>'+item.itemname+"</td>":"<td>"+item.itemname+"</td>";
                $string+="<td>"+item.bn+"</td>";
                $string+="<td><span class='itemprice' value='price' id="+item.itemid+">"+item.price+"</span></td>";
                $string+="<td><span class='supprice' value='supprice' id="+item.itemid+">"+item.supperprice+"</span></td>";
                $string+="<td><span class='inventory' value='inventory' id="+item.itemid+">"+item.inventory+"</span></td>";
                $string+="<td>"+item.belong+"</td>"; 
                $string+="<td>"+item.createtime+"</td>";      
                $string+='<td><div class="am-btn-toolbar"><div class="am-btn-group am-btn-group-xs">';
                $string+='<span class="am-icon-eye"></span><a href=/shop.php/Home/Admin/viewItem?itemid='+item.itemid+'>查看</a>';
                $string+="</div></div></td></tr>"; 
              });
              $tbody.append($string);
              $($tbody).replaceAll("tbody");
              //替换分页
              $(".count").html($page.count);
              $(".currentpage").html($page.page);
              $(".pagetotal").html($page.pagetotal);
              $("#pagesize").val($page.pagesize);
              $('.prepage').attr('value',$page.prevpage);
              $('.nexpage').attr('value',$page.nextpage);
              $('.laspage').attr('value',$page.pagetotal);

            }else{
              var $page=message.pagearr; //底部分页
              $(".pagetotal").html($page.pagetotal);
              $(".count").html($page.count);
             $('tbody').html('暂无数据');
           }
         }

       });
    }
  //筛选数据
  $("#filter").click(function(){
    $.searchitem();
  })
  //关键字搜索产品
  $('.keyseach').click(function(){
    var keywords=$("input[name='keywords']").val();
    $.searchitem(1,keywords);
    return false;
  });
})(jQuery)
/*商品弹出层(规格)特效*/
 ;(function($){
  $.ItemLayer=
  {
    addLayer:function(e)
    {
         
         var itemid=(e!=false)?e:0; //商品ID 
         //获取商品的颜色，价格，库存，名称，型号
         $.ajax({
          type:'post',
          url:"/shop.php/Home/Admin/ajaxProduct",
          data:{'itemid':itemid},
          dataType:"json",
          success:function(message)
          {
            var $tbody=$('<tbody></tbody>');
            var $string='';
            var status=message.status;
            var $products=message.products;
            if(status)
            {
              $.each($products,function(key,product)
              {
                $string+='<tr><td>'+product.productid+'</td>';
                $string+='<td> <span class="prodcutcolor" value="prodcutcolor" id="'+product.productid+'">'+ product.color+'</span></td>';
                $string+='<td> <span class="prodcutprice" value="prodcutprice" id="'+product.productid+'">'+ product.price+'</span></td>';
                $string+='<td> <span class="prodcutsupprice" value="prodcutsupprice" id="'+product.productid+'">'+ product.supprice+'</span></td>';
                $string+='<td> <span class="productinventory" value="productinventory" id="'+product.productid+'">'+ product.inventory+'</span></td>';
                $string+='</tr>';
              })
                        //设置弹出层的itemid 的值
               $("input[name='itemidvalue']").val(itemid);
               $tbody.append($string);
               $($tbody).replaceAll(".protable tbody");
            }else
            {
              var $tbody=$('<tbody>暂无规格</tbody>');
              $($tbody).replaceAll(".protable tbody");
            }
          }
        });         
    }, //弹出product结束
  }
   
      //为商品绑定弹出层事件
      $(document).on('click','.bindaddLayer',function(){
        var $this=$(this);
        var itemid=$this.attr('value');
        
        $('#pro_spec').modal();
        
        
        $.ItemLayer.addLayer(itemid);
      });


  })(jQuery)